<?php


namespace app\admin\model;


use app\common\model\ArticleBaseModel;
use app\Exception\DbException;
use think\db\BaseQuery;
use think\facade\Db;

/**
 * @property mixed id
 */
class ArticleModel extends ArticleBaseModel
{

    /**
     * 添加文章
     * @param array $param
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function addArticle(array $param)
    {
        $param['add_time'] = getDateTime();
        $param['update_time'] = getDateTime();
        $param['article_consult'] = json_encode($param['article_consult']);
        $arrTag = $param['article_tag'];
        unset($param['article_tag']);
        $param = $this->setArticleImageFiled($param, $arrTag[count($arrTag)-1]);
        dbTrans(function () use ($arrTag, $param) {
            $this->save($param);
            $id = $this->id;
            $this->find($id)->articleTag()->saveAll($arrTag);
        });
    }

    /**
     * 删除文章
     * @param int $id
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function deleteArticle(int $id)
    {
        dbTrans(function () use ($id) {
            ArticleModel::destroy($id);
            (new ArticleTagModel())->where('article_id', '=', $id)->delete();
        });
    }

    /**
     * 通过id获取文章
     * @param int $id
     * @param array $hidden
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getArticleById(int $id, array $hidden = [])
    {
        return $this->with(['articleCategory' => function (BaseQuery $query) {
            $query->visible(['id', 'category_name']);
        }, 'articleTag' => function (BaseQuery $query) {
            $query->hidden([]);
        }])->where('id', '=', $id)->find()->hidden($hidden);
    }

    /**
     * 更新文章
     * @param array $paramArr
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function updateArticle(array $paramArr)
    {
        $param = $this->setArticleImageFiled($paramArr, $paramArr['article_tag'][0]);
        $param['article_consult'] = json_encode($param['article_consult']);
        $param['update_time'] = getDateTime();
        dbTrans(function () use ($param) {
            $this->find($param['id'])->save($param);
            (new ArticleTagModel())->deleteTagByArticleId($param['id']);
            $this->find($param['id'])->articleTag()->saveAll($param['article_tag']);
        });
    }

    /**
     * 如果文章的图片为空，则把第一个标签作为图片
     * @param $param
     * @param $tagId
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    private function setArticleImageFiled($param, $tagId) {
        if ($param['article_image'] ?? true || $param['article_image'] === null) {
            $tagModel = new TagModel();
            $article_image = $tagModel->getTagImageById($tagId);
            $param['article_image'] = $article_image;
        }
        return $param;
    }
}